conf = require("../config")
if not conf.mason.enabled then return {} end

language_servers = {
  'clangd',
  'basedpyright',
  'lua-language-server',
  -- 'java-language-server',
  -- 'svls',
  'svlangserver',
  'texlab', -- latex
  'marksman',
  'rust-analyzer',
  'json-lsp',
}

vim.api.nvim_create_autocmd("User", {
  pattern = "LazyDone",
  group = vim.api.nvim_create_augroup('mason-lsp', {clear = true}),
  callback = function()
    local mr = require("mason-registry")
    local has_mr, mr = pcall(require, "mason-registry")
		if  has_mr then
			-- local mr = require("mason-registry")
      -- local has_mr, mr = pcall(require, "mason-registry")
			mr:on("package:install:success", function()
				vim.defer_fn(function()
					-- trigger FileType event to possibly load this newly installed LSP server
					require("lazy.core.handler.event").trigger({
							event = "FileType",
							buf = vim.api.nvim_get_current_buf(),
						})
				end, 100)
			end)

			mr.refresh(function()
				for _, tool in ipairs(language_servers) do
					local p = mr.get_package(tool)
					if not p:is_installed() then
						p:install()
					end
				end
			end)
		end
  end
})

return {
  {
    "williamboman/mason.nvim",
    cmd = "Mason",
    opts = {
      ui = {
        border = "single",
        icons = {
          package_installed = "✓",
          package_pending = "➜",
          package_uninstalled = "✗"
        }
      }
    }
  },
  -- {
  --   "williamboman/mason-lspconfig.nvim",
  --   config = function()
  --     require('mason-lspconfig').setup({
  --       ensure_installed = language_servers,
  --     })
  --   end,
  -- },
}
